package com.gykfbxxh.imovie.view.main.activity;

import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.utils.util.LogUtils;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.BaseActivity;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.bean.film.FilmListBean;
import com.gykfbxxh.imovie.bean.film.FilmListEntity;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.custom.view.likenolike.CardConfig;
import com.gykfbxxh.imovie.custom.view.likenolike.LikeNoLikeCallback;
import com.gykfbxxh.imovie.custom.view.likenolike.OnLikeNoLikeListener;
import com.gykfbxxh.imovie.custom.view.likenolike.OverLayCardLayoutManager;
import com.gykfbxxh.imovie.greendao.GDManager;
import com.gykfbxxh.imovie.greendao.bean.LikeLookGD;
import com.gykfbxxh.imovie.greendao.gen.LikeLookGDDao;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.manager.AppConfigManager;
import com.gykfbxxh.imovie.manager.AppManager;
import com.gykfbxxh.imovie.utils.HttpUtils;
import com.gykfbxxh.imovie.utils.glide.ImageLoader;
import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import static com.gykfbxxh.imovie.R.id.rv;

public class LikeNoLikeActivity extends BaseActivity {
    private RecyclerView mRv ;
    private ImageView tv_exit;
    private CommonAdapter<FilmListEntity> mAdapter;
    private List<FilmListEntity> datas = new ArrayList<>();
    private TextView tv_nodata ;
    private ImageView iv_left,iv_right;
    private RelativeLayout rl_zhezhao_today;
    private ImageView iv_today_anim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSlideBack = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_like_no_like);

        initView();
        initData();
    }

    private void initView() {
        rl_zhezhao_today = (RelativeLayout)findViewById(R.id.rl_zhezhao_today);
        iv_today_anim = (ImageView)findViewById(R.id.iv_today_anim);
        mRv = (RecyclerView) findViewById(rv);
        tv_exit = (ImageView)findViewById(R.id.tv_exit);
        tv_nodata = (TextView)findViewById(R.id.tv_nodata);
        iv_left = (ImageView)findViewById(R.id.iv_left);
        iv_right = (ImageView)findViewById(R.id.iv_right);
        iv_left.setAlpha(0f);
        iv_right.setAlpha(0f);
        tv_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity();
            }
        });
        rl_zhezhao_today.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rl_zhezhao_today.setVisibility(View.GONE);
                if (anim != null && anim.isRunning()) {
                    //停止蹦跑动画
                    anim.stop();
                }
            }
        });
        showAnim();
    }

    private AnimationDrawable anim;
    private void showAnim() {

        //第一次进入才显示,以后不显示
        SharedPreferences sp = AppConfigManager.getInstance().getConfigSP();
        boolean isShow = sp.getBoolean(ConfigKey.isShow_today, false);
        if(!isShow) {
            rl_zhezhao_today.setVisibility(View.VISIBLE);
            anim = null;
            anim = (AnimationDrawable) iv_today_anim.getDrawable();
            iv_today_anim.post(new Runnable() {

                @Override
                public void run() {
                    anim.start();
                }
            });
            iv_today_anim.setDrawingCacheBackgroundColor(0xffff0000);//设置RPG565节约内存

            SharedPreferences.Editor edit = sp.edit();
            edit.putBoolean(ConfigKey.isShow_today,true);
            edit.commit();
        }
    }

    private void initData() {
        HttpUtils.addGetRequest(URIContant.FILMLIST+"1", new TypeToken<FilmListBean>() {
        }.getType(), mPageName, new HttpListener<FilmListBean>() {


            @Override
            public void onError(VolleyError error) {
            }

            @Override
            public void onSuccess(FilmListBean response) {
                LogUtils.e("xuxh","onSuccess");
                if(response!=null&&response.getResult()!=null){
                    FilmListBean.ResultBean result = response.getResult();
                    if(result!=null) {
                        List<FilmListEntity> list = result.getList();
                        if(list!=null&&list.size()>0) {
                            //需要加一层数据判断****************
                            clearOtherData(list);//过滤已想看数据
                            datas.addAll(list);
                            initAdapter();
                            if(list.size()==0) {
                                tv_nodata.setVisibility(View.VISIBLE);
                            }else {
                                tv_nodata.setVisibility(View.GONE);
                            }
                        }else {
                            tv_nodata.setVisibility(View.VISIBLE);
                        }
                    }else {
                        tv_nodata.setVisibility(View.VISIBLE);
                    }
                }else {
                    tv_nodata.setVisibility(View.VISIBLE);
                }
            }


        });
    }

    private void clearOtherData(List<FilmListEntity> list) {
        List<LikeLookGD> list1 = null;
        try {
            list1 = GDManager.getInstance().getSession().getLikeLookGDDao().queryBuilder().build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(list1!=null&&list1.size()>0) {
            for (int i =0;i<list.size();i++){
                FilmListEntity listBean = list.get(i);
                for(int j=0;j<list1.size();j++){
                    if(listBean.getFObjectid().equals(list1.get(j).getFObjectid())) {
                        list.remove(i);
                        i--;
                        break;
                    }
                }
            }
        }
        LogUtils.e("xuxh");
    }

    private void initAdapter() {
        mRv.setLayoutManager(new OverLayCardLayoutManager());
        mRv.setAdapter(mAdapter = new CommonAdapter<FilmListEntity>(this, datas, R.layout.item_swipe_card) {
            public static final String TAG = "zxt/Adapter";

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LogUtils.e("xuxh", "onCreateViewHolder() called with: parent = [" + parent + "], viewType = [" + viewType + "]");
                return super.onCreateViewHolder(parent, viewType);
            }

            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                LogUtils.e("xuxh", "onBindViewHolder() called with: holder = [" + holder + "], position = [" + position + "]");
                super.onBindViewHolder(holder, position);
            }

            @Override
            public void convert(ViewHolder viewHolder, FilmListEntity listBean) {
                LogUtils.e("xuxh", "convert() called with: viewHolder = [" + viewHolder + "], swipeCardBean = [" + listBean.getFName() + "]");
                viewHolder.setText(R.id.tv_title, listBean.getFName());
                if(listBean.getFObject()!=null) {
                    viewHolder.setText(R.id.tv_type,listBean.getFObject().getFFilmtypename());
                    ImageLoader.getInstance().loadImage2Cache(LikeNoLikeActivity.this,(ImageView) viewHolder.getView(R.id.iv),listBean.getFObject().getFPhoto());
                }
            }
        });

        CardConfig.initConfig(this);

        final LikeNoLikeCallback callback = new LikeNoLikeCallback(mRv, mAdapter, datas);
        callback.setOnLikeNoLikeListener(new OnLikeNoLikeListener() {
            @Override
            public void onLike(FilmListEntity swipeCardBean) {
                iv_left.setAlpha(0f);
                iv_right.setAlpha(0f);
                if(swipeCardBean!=null) {
                    FilmEntity filmEntity = swipeCardBean.getFObject();
                    if(filmEntity==null) {
                        return;
                    }
                    if(!TextUtils.isEmpty(filmEntity.getFId())) {
                        long l = System.currentTimeMillis();
                        LikeLookGD likeLook = new LikeLookGD(l,filmEntity.getFId(),filmEntity.getFName(),filmEntity.getFPhoto());
                        LikeLookGDDao likeLookGDDao = GDManager.getInstance().getSession().getLikeLookGDDao();
                        try {
                            likeLookGDDao.insertOrReplace(likeLook);//插入数据库
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

            @Override
            public void onNoLike(FilmListEntity swipeCardBean) {
                iv_left.setAlpha(0f);
                iv_right.setAlpha(0f);
            }

            @Override
            public void onLastRemove() {
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void onMovieRightAlpha(float xFraction) {
                iv_right.setAlpha(0.5f+xFraction);
                iv_left.setAlpha(0f);
            }

            @Override
            public void onMovieLeftAlpha(float xFraction) {
                iv_left.setAlpha(0.5f+xFraction);
                iv_right.setAlpha(0f);
            }

            @Override
            public void onMovieAlpha0() {
                iv_right.setAlpha(0f);
                iv_left.setAlpha(0f);
            }
        });
        //测试竖直滑动是否已经不会被移除屏幕
        //callback.setHorizontalDeviation(Integer.MAX_VALUE);

        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(mRv);
    }
}
